<template>
  <div
    ref="article-list"
    class="srticle-list"
  >
    <van-pull-refresh 
      v-model="isRefreshLoading"
      :success-text="refreshSuccessText"
      :success-duration="1500"
      @refresh="onRefresh"
    > 
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- <van-cell
          v-for="(article,index) in articles"
          :key="index"
          :title="article.title"
        /> -->
        <article-item 
          v-for="(article,index) in articles"
          :key="index"
          :article="article"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
	import { getArticles } from '../../../api/article'
	import ArticleItem  from  '../../../components/article-item'
	import { debounce } from 'lodash'
 	export default{
		name:'ArticleList',
		components:{
		   ArticleItem
		},
		props:{
			channel:{
				type:Object,
				required:true
			}
		},
		data(){
			return{
				 articles: [],//数据列表
				 loading: false,//控制加载中的loading状态
				 finished: false,
				 // 控制加载结束的状态,当加载结束,不在触发加载更多
			     timestamp:null,//获取下一页数据的时间戳,
				 isRefreshLoading:false,//控制下拉刷新的状态
				 refreshSuccessText:'',//下拉刷新成功提示
				 scrollTop:0,//列表滚动到顶部的距离
			}
		},
		computed:{},
		watch:{},
		created(){},
		mounted(){
			const articleList = this.$refs['article-list']
			articleList.onscroll = debounce(() => {
				this.scrollTop = articleList.scrollTop
			},50)
		},
		activated() {
			console.log('从缓存中被激活')
			this.$refs['article-list'].scrollTop = this.scrollTop
		},
		deactivated() {
			console.log('组件失去活动了')
		},
		methods:{
			async onLoad(){
				// console.log('onLoad')
				 //1.请求获取数据
				const { data } = await getArticles({
					channel_id:this.channel.id,//频道id,
					timestamp:Date.now() || this.timestamp,//时间戳，请求新的推荐数据传当前的时间戳，
					// 请求历史推荐传指定的时间戳
					//下一页数据使用上一页返回数据中的时间戳，打印出来你就知道了
					with_top:1//是否包含置顶，进入页面第一次请求时要包含置顶文章，
					// 1-包含置顶，0-不包含
				})
				// console.log(data)
				//2.把数据放到list数组中
				const { results } = data.data
				// const  result  = data.data
				// console.log(results)//将data.data的results结构出来，获得的只有results
				// console.log(result)//将data.data赋值给变量result
				this.articles.push(...results)
				//3.设置本次加载状态结束，
				// 他才可以判断是否需要加载下一次，否侧就会永远的停在这里
				this.loading = false
				// 4.数据会全部加载完成
				if(results.length){
					//更新获取下一页数据的页码
					this.timestamp = data.data.pre_timestamp
				}else{
					//没有数据了，把加载状态设置结束，不在触发加载更多
				 this.finished =true
				}
			},
			async onRefresh() {
				const { data } = await getArticles({
					channel_id:this.channel.id,//频道id,
					timestamp:Date.now(),
					//更新需要的是当前时间
					with_top:1
				})
				//更行是将数据加载在上方
				const { results } = data.data
				this.articles.unshift(...results)
				//更改状态
				this.isRefreshLoading = false
				this.refreshSuccessText = `更新了${results.length}条数据`
			}
		}
 }
</script>

<style>
	.srticle-list{
		position: fixed;
		left: 0;
		right: 0;
		top: 90px;
		bottom: 50px;
		overflow-y: auto;
			}
</style>
